/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Lid;
import model.Rit;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistence.SessionManager;

public class RittenOverzichtController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String address;

        // Haal de huidige sessie op
        HttpSession sessie = request.getSession();

        // Hier volgt de 5 bovenknoppen active array, in gebruik in include/top_nav.jsp
        Boolean[] topNavActive = new Boolean[]{
            false, true, false, false, false};
        request.setAttribute("topNavActive", topNavActive);

// is user ingelogd?
        if (null == sessie.getAttribute("currentUser")) {
// User IS NOT logged in....    
            System.out.println("User is niet ingelogd - Redir naar teaser pagina");
            address = "mijnTeaser.jsp";
        } else {
// User IS logged in....

            //huidige lid uit de sessie halen
            Lid lid = (Lid) sessie.getAttribute("currentUser");
            String vanProvincie = lid.getLidProvincie();

            String hql = "from Rit where RIT_VAN_PROVINCIE = '" + vanProvincie + "' AND RIT_DELETED = false AND RIT_DATUM >= current_date ORDER BY RIT_DATUM asc, RIT_TIJD asc";

            Session session = null;
            session = SessionManager.openSession();

            Transaction tx = session.beginTransaction();
            List<Rit> ritten = session.createQuery(hql).list();
            session.close();

            // Array(list) meesturen naar rittenoverzicht 
            request.setAttribute("ritten", ritten);
            request.setAttribute("aantalRitten", ritten.size());
            request.setAttribute("currentuser", lid.getLidId());

            address = "/ritten.jsp";

        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);


    }
}
